package com.soar.recruitment.module.user.bll.controller;


import android.text.TextUtils;

import com.soar.framework.net.http.OkHttpUtils;
import com.soar.recruitment.common.bll.LoginManager;
import com.soar.recruitment.common.bll.SOARHttpCallback;
import com.soar.recruitment.common.dal.model.SOARBaseModel;
import com.soar.recruitment.common.util.MyToast;
import com.soar.recruitment.module.user.bll.api.UserApi;
import com.soar.recruitment.module.user.dal.bean.UserModel;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;

public class RegisterPresenter implements RegisterContrats.Presenter {

    // Http - tag
    private final static String TAG_REGISTER = "register";

    private RegisterContrats.View mView;
    private UserApi mUserApi;

    public RegisterPresenter(RegisterContrats.View view) {
        this.mView = view;
        this.mUserApi = new UserApi();
    }

    private String captcha;

    @Override
    public void register() {
        String member_phone = mView.getPhoneNum();
        String member_password = mView.getPwdNum();
        String register_code = mView.getCodeNum();

        if(TextUtils.isEmpty(member_phone)) {
            MyToast.show("手机号码不能为空");
            return;
        }

        if(TextUtils.isEmpty(register_code)) {
            MyToast.show("验证码不能为空");
            return;
        }

        if(TextUtils.isEmpty(member_password)) {
            MyToast.show("密码不能为空");
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("member_phone", member_phone);
        params.put("member_password", member_password);
        params.put("register_code", register_code);
        params.put("register_type", "mobile");

        if(!TextUtils.isEmpty(captcha)) {
            params.put("captcha", captcha);
        }

        mUserApi.register(TAG_REGISTER, params, new SOARHttpCallback<SOARBaseModel<UserModel>>() {

            @Override
            public void onBefore(Request request, int id) {
                super.onBefore(request, id);
                if (mView != null) {
                    mView.showLoading("注册中...");
                }
            }

            @Override
            public void onRequestSuccess(Object response) {
                UserModel userModel = (UserModel) response;
                if (userModel != null) {
                    // save user data
                    LoginManager.loginSuccess(userModel);
//                    mView.goNext();

                    // FIXME: 2017/7/9  close page come to mainView


                    if (mView != null) {
                        mView.goNext();
                    }
                } else {
                    MyToast.show("数据错误~");
                }
            }

            @Override
            public void onAfter(int id) {
                super.onAfter(id);
                if (mView != null) {
                    mView.closeLoading();
                }
            }
        });
    }

    @Override
    public void destory() {
        OkHttpUtils.getInstance().cancelTag(TAG_REGISTER);
    }

}
